<?php

/**
 *    限时促销活动控制器
 *
 *    @author    yanghj
 *    @date      2015.04.24
 */
class ActivityApp extends MallbaseApp
{
    var $_activity_mod;
    var $_activity_goods_mod;
    var $_goods_mod;

    /* 构造函数 */

    function __construct()
    {
        $this->ActivityApp();
    }

    function ActivityApp()
    {
        parent::__construct();

        $this->_activity_mod = & m('activity');
        $this->_activity_goods_mod = & m('activitygoods');
        $this->_goods_mod = & m('goods');
    }
    
    /**
     *    活动列表
     *
     */   
    function index()
    {
        $adv_mod =& m('banner');
        $this->assign('adv_list', $adv_mod->_get_ad(array(11)));//促销活动的广告图片
        $this->assign('category_list', $adv_mod->_get_ad(array(12)));//促销活动的精品分类
        $this->assign('activity_goods', $this->_get_activity_goods());//取得活动期间的10个商品
        $this->assign('wl_goods', $this->_get_wl_goods());//取得万菱闪购活动期间的3个商品
        $this->_config_seo('title', Conf::get("site_name") . ' - 促销活动');
        $this->display('activity.index.html');
    }

    /**
     * 取得活动期间的10个商品
     */
    function _get_activity_goods()
    {
        //$sql = "SELECT act_name FROM {$this->_activity_mod->table} WHERE act_type = 1 group by act_name ";
        //$act_name_arr = $this->_activity_mod->getCol($sql);
        $act_name_arr = array('0'=>'限时促销','1'=>'厂家直供','2'=>'限量爆款','3'=>'清仓特卖','4'=>'最新上架');
        $act_goods = array();
        if($act_name_arr)
        {
            foreach($act_name_arr  as $key=>$act_name)
            {
                $sql_str = "SELECT g.goods_id,g.goods_name,g.default_image,g.shipping_id,g.price,g.promote_price,a.act_name,a.free_shipping, s.store_id, s.store_name FROM {$this->_activity_goods_mod->table} AS ag left join {$this->_goods_mod->table} g on ag.goods_id = g.goods_id left join {$this->_activity_mod->table} a on a.act_id = ag.act_id left join " .DB_PREFIX."store s on g.store_id = s.store_id WHERE g.closed = 0 AND g.if_show = 1 AND a.act_name = '{$act_name}' AND a.start_time < ".gmtime()." AND a.end_time > ".gmtime()." AND ag.goods_title = '' ORDER BY ag.sort_order DESC, g.price ASC LIMIT 10";
                $arr = $this->_activity_goods_mod->getAll($sql_str);
                if($arr)
                {
                    $act_goods[$key]['act_name'] = $act_name;
                    $act_goods[$key]['goods'] = $this->_activity_goods_mod->getAll($sql_str);
                }
            }
        }

        return $act_goods;
    }

    /**
     * 取得万菱闪购活动期间的3个商品
     */
    function _get_wl_goods()
    {
        $sql_str = "SELECT g.goods_id,g.goods_name,g.default_image,g.shipping_id,g.price,g.market_price,g.promote_price,ag.promote_discount,ag.goods_title,ag.goods_desc,ag.goods_img,ag.total_number,a.free_shipping,a.end_time FROM {$this->_activity_goods_mod->table} AS ag left join {$this->_goods_mod->table} g on ag.goods_id = g.goods_id left join {$this->_activity_mod->table} a on a.act_id = ag.act_id WHERE g.closed = 0 AND g.if_show = 1 AND a.act_type = 1 AND a.start_time < ".gmtime()." AND a.end_time > ".gmtime()." AND ag.goods_title != '' ORDER BY ag.sort_order DESC, g.price ASC LIMIT 3";

        $arr = $this->_activity_goods_mod->getAll($sql_str);
        if($arr)
        {
           foreach($arr as $key=>$val)
           {
               $arr[$key]['goods_discount'] = $val['promote_discount'] > 0 ? floatval(sprintf("%.1f",$val['promote_discount']*10)) : floatval(sprintf("%.1f",$val['promote_price']/$val['price']*10));
               $arr[$key]['life_time'] = $val['end_time'] - gmtime();
           }
        }
        return $arr;
    }



}

?>